package com.taomeng.dao.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.taomeng.dao.RoleDao;
import com.taomeng.entity.PageBean;
import com.taomeng.entity.Permission;
import com.taomeng.entity.Role;
import com.taomeng.entity.User;
import com.taomeng.util.JdbcTemplateSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RoleDaoImpl implements RoleDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private JdbcTemplateSupport jdbcTemplateSupport;

    @Override
    public PageBean<Role> findByPage(String page, String limit) {
        StringBuilder sb = new StringBuilder("select * from t_role");

        if (null == page && null == limit) {
            PageBean<Role> pageBean = new PageBean<>();
            List<Role> roleList = jdbcTemplate.query(sb.toString(), new BeanPropertyRowMapper<>(Role.class));
            pageBean.setPageSize(roleList.size());
            pageBean.setList(roleList);
            return pageBean;
        }
        Page<Role> pagination = PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));


        /**
         * 判断模糊搜索的key是否为空，如果是空则直接进行搜索
         * 否则进行模糊搜索
         */
        try {
            return jdbcTemplateSupport.queryForPage(
                    sb.toString(),
                    pagination,
                    new BeanPropertyRowMapper<>(Role.class));
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public List<Role> findRoleByUserId(Long id) {
        String sql = "select t_role.id,t_role.name,t_role.sn " +
                "from t_user,t_user_role,t_role " +
                "where t_user.id=t_user_role.user_id and t_role.id=t_user_role.role_id " +
                "and t_user.id = ?";
        Object[] params = {
                id
        };
        try {
            return jdbcTemplate.query(sql, params, new BeanPropertyRowMapper<>(Role.class));
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean add(Role role) {
        String sql = "insert into t_role values(?,?,?)";
        Object[] params = {
                role.getId(),
                role.getName(),
                role.getSn()
        };

        try {
            return jdbcTemplate.update(sql, params) == 1;
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean delById(Long id) {
        String sql = "delete from t_role where id = ?";
        Object[] params = {id};
        try {
            return jdbcTemplate.update(sql, params) == 1;
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List findUsersByRoleId(Long id) {
        String sql = "select \n" +
                "\tt_user_role.user_id\n" +
                "from \n" +
                "\tt_user_role,\n" +
                "\tt_user\n" +
                "where \n" +
                "\tt_user_role.user_id=t_user.id\n" +
                "\tand\n" +
                "\tt_user_role.role_id = ? ";
        Object[] params = {id};
        try {
            return jdbcTemplate.queryForList(sql,params,Long.class);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}